package com.code;

//** This example is part of the iText 7 tutorial.*/

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.File;
import java.io.IOException;

/**
 * Simple drawing lines example.
 */
@WrapToTest
public class C02E01_Axes {

    public static final String DEST = "D:\\测试pdf\\day1_5_3.pdf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C02E01_Axes().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException {

        //准备pdf
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        PageSize ps = PageSize.A4.rotate();
        PdfPage page = pdf.addNewPage(ps);

        PdfCanvas canvas = new PdfCanvas(page);
        //坐标系的远点替换成页面
        canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2);

        //C02E01_Axes.drawAxes(canvas, ps);
        C02E01_Axes.drawsquare(canvas,ps);
        //关闭文档
        pdf.close();

    }

    public static void drawAxes(PdfCanvas canvas, PageSize ps) {
        //绘制X轴
        canvas.moveTo(-(ps.getWidth() / 2 - 15), 0)
                .lineTo(ps.getWidth() / 2 - 15, 0)
                .stroke();

        //绘制X轴箭头
        canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
                .moveTo(ps.getWidth() / 2 - 25, -10)
                .lineTo(ps.getWidth() / 2 - 15, 0)
                .lineTo(ps.getWidth() / 2 - 25, 10).stroke()
                .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER);

        //绘制Y轴
        canvas.moveTo(0, -(ps.getHeight() / 2 - 15))
                .lineTo(0, ps.getHeight() / 2 - 15)
                .stroke();

        //绘制Y轴箭头
        canvas.saveState()
                .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
                .moveTo(-10, ps.getHeight() / 2 - 25)
                .lineTo(0, ps.getHeight() / 2 - 15)
                .lineTo(10, ps.getHeight() / 2 - 25).stroke()
                .restoreState();

        //Draw X serif
        for (int i = -((int) ps.getWidth() / 2 - 61); i < ((int) ps.getWidth() / 2 - 60); i += 40) {
            canvas.moveTo(i, 5).lineTo(i, -5);
        }
        //Draw Y serif
        for (int j = -((int) ps.getHeight() / 2 - 57); j < ((int) ps.getHeight() / 2 - 56); j += 40) {
            canvas.moveTo(5, j).lineTo(-5, j);
        }
        canvas.stroke();
    }
    public static void drawsquare(PdfCanvas canvas, PageSize ps){
        //正方形的两条横线
       /* canvas.moveTo(-(ps.getWidth()/4-15),-(ps.getHeight()/4-15))//起点
                .lineTo(ps.getWidth()/4-15,-(ps.getHeight()/4-15))//终点
                .moveTo(-(ps.getWidth()/4-15),ps.getHeight()/4-15)
                .lineTo(ps.getWidth()/4-15,ps.getHeight()/4-15)
                .stroke();//画线
        canvas.moveTo(-(ps.getWidth()/4-15),-(ps.getHeight()/4-15))
                .lineTo(-(ps.getWidth()/4-15),ps.getHeight()/4-15)
                .moveTo(ps.getWidth()/4-15,-(ps.getHeight()/4-15))
                .lineTo(ps.getWidth()/4-15,ps.getHeight()/4-15)
                .stroke();*/
        canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND)
                .moveTo(-(ps.getWidth()/4-15),-(ps.getHeight()/4-15))
                .lineTo(ps.getWidth()/4-15,-(ps.getHeight()/4-15))
                .lineTo(ps.getWidth()/4-15,ps.getHeight()/4-15)
                .stroke()
                .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER);

    }
}